package com.example.polarity;

import java.util.ArrayList;
import java.util.Random;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.ViewGroup;

public class CircularAstroids extends ViewGroup 
{
	
	int ScreenWidth = 0;
	int ScreenHeight = 0;
	int maxAstroids = 90;
	Matrix matrix;
	
	int rotation = 0;
	int DrawCount = 0;
	boolean init = true;
	public ArrayList<AstroidObject> AstroidtList = new ArrayList<AstroidObject>();
	
	public CircularAstroids(Context context) 
	{	
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	@SuppressLint("NewApi")
	public CircularAstroids(Context context, AttributeSet attrs) 
	{
		super(context, attrs);

		TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.CircularAstroids);
		
		this.maxAstroids =  a.getInt(R.styleable.CircularAstroids_AstroidCount,90);
		
		this.setWillNotDraw(false);
		matrix = new Matrix();


	}
	
	public CircularAstroids(Context context, AttributeSet attrs, int defStyle) 
	{
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) 
	{
		
		if(init == true)
		{
			for(int i = 0; i < this.maxAstroids; i++)
			{
				AstroidObject a = new AstroidObject(this.getContext());
				//AstroidObject a = ;
				
				Random rand = new Random();
				a.pivotX = rand.nextInt( 200 - 120) + 120;
				a.setY(this.getHeight() / 2);
				a.DrawPosition = (i + 1) * 4;
				
				addView(a);
			}
		}
	}
	
	 @SuppressLint("NewApi")
	 @Override
	 protected void onDraw(Canvas canvas) 
	 {
		 
		 this.DrawCount++;
		 
		 for(int i = 0; i < this.getChildCount(); i++)
		 {
			
			AstroidObject a = (AstroidObject)this.getChildAt(i);
			
			if(a.DrawPosition < this.DrawCount)
				a.rotation++;
			
			matrix. setRotate(a.rotation, ((this.getWidth() / 2)) - a.pivotX  , (this.getWidth() / 2) - a.pivotX);

			//matrix.postTranslate(a.pivotX, a.pivotX );
			matrix.postTranslate(a.pivotX, a.pivotX );
		    canvas.drawBitmap(a.AstroidImage, matrix, null ); 
		    
			 
			 if(a.rotation == 360)
				 a.rotation = 1;
		 } 
		 invalidate();

	 }




}
